import datetime
import time

from curl_cffi import requests


def get_all(page_size=20,page_num=1):
    url = "https://55.push2.eastmoney.com/api/qt/clist/get"

    params = {
        'pn': f"{page_num}",
        'pz': f"{page_size}",
        'po': "1",
        'np': "1",
        'fltt': "2",
        'invt': "2",
        'dect': "1",
        'wbp2u': "|0|0|0|web",
        'fid': "f22",
        'fs': "m:0 t:6,m:0 t:80,m:1 t:2,m:1 t:23,m:0 t:81 s:2048",
        'fields': "f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152",
        '_': f"{int(time.time() * 1000)}"
    }

    response = requests.get(url, params=params)

    return response.json()


def get_k_lines(code):
    url = "https://push2his.eastmoney.com/api/qt/stock/kline/get"

    params = {
        'secid': code,
        'fields1': "f1,f2,f3,f4,f5,f6",
        'fields2': "f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61",
        'klt': "101",
        'fqt': "1",
        'end': "20500101",
        'lmt': "120",
        '_': f"{int(time.time() * 1000)}"
    }

    response = requests.get(url, params=params)

    return response.json()


def get_zt_pool():
    url = "https://push2ex.eastmoney.com/getTopicZTPool"
    params = {
        "ut": "7eea3edcaed734bea9cbfc24409ed989",
        "dpt": "wz.ztzt",
        "Pageindex": "0",
        "pagesize": "170",
        "sort": "fbt:asc",
        "date": f"{datetime.datetime.now().strftime('%Y%m%d')}",
        "_": f"{int(time.time() * 1000)}"
    }
    response = requests.get(url, params=params)
    return response.json()


def get_dates(days=5):
    k_line = get_k_lines("1.000001")
    return [i.split(',')[0] for i in k_line['data']['klines'][::-1]][1:days]


def get_symbol_data(secids: str):
    """获取指定股票的数据"""
    url = "https://push2.eastmoney.com/api/qt/ulist/get"
    params = {
        "fields": "f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152",
        "secids": secids,
        "pn": "1",
        "pz": "200",
        "_": f"{int(time.time() * 1000)}"
    }
    response = requests.get(url, params=params)
    return response.json()


def gb_rank():
    params = {
        "count": "200",
        "tag": "2",
        "token": "1D50F6FB3B72F7D5F478A23B9BE911DB",
        "_": f"{int(time.time() * 1000)}",
        "PageIndex": "2"
    }
    resp = requests.get(
        "https://searchapi.eastmoney.com/api/HotKeyword/Get",
        params=params
    )

    return resp.json()


if __name__ == '__main__':
    print(gb_rank())
